<?php
namespace jy_renwushop;
defined('BY_JYA') or exit('error');
/*
	任务解锁
*/
class renwu extends \plugin{
	/*
		downs
	*/
	public function canyu($params,$pinggoods=array()){
		$basic=$this->plugin_setting('basic');
		if($basic['task_open']!=1){
			return false;
		}
		$fromuser=pdo_getcolumn('jy_renwushop_member',array('mid'=>$params['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		//升级
		$this->model('member')->update_agent_level($fromuser);
		return false;
	}
	public function canfen($order){
		$fenxiaoset=plugin('jy_commission')->plugin_setting('setting');
		if($fenxiaoset['open_otherfee']!=1){
			return false;
		}
		if(strpos($fenxiaoset['otherfee_types'],';jy_renwushop;') !== false){ 
		}else{
			return false;
		}
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'orderid'=>$order['id'],
			'order_price'=>$order['fee'],
			'commission_money'=>0,
		);
		$fromuser=pdo_getcolumn('jy_commission_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'agent_id');
		//当分销层级大于0时，逐次寻找上级推荐人并计算佣金
		$setlevel=intval($fenxiaoset['level'])?:2;
		for($i=0;$i<$setlevel;$i++){
			if(!$fromuser){
				break;
			}
			$member=pdo_get('jy_commission_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$level=pdo_get('jy_commission_level',array('id'=>$member['level_id']));
			$level=$level?:array();
			$fromuser=$member['agent_id'];
			$this->create_commission($member,$level,$i+1);
		}
		return false;
	}
	/*
		分销佣金;
	*/
	function create_commission($agent,$level=array(),$i=0){
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				return false;
			}else{
				$level_id=$level['id'];
				$commissions=json_decode($level['commissions'],true);
				$commission_per=$commissions['otherjy_renwushop'.$i];
			}			
			$money=0;
			$order_price=$this->commission_order['order_price'];		
			$money=$order_price*$commission_per/100;
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$i,
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>'jy_renwushop',
				);
				pdo_insert('jy_commission_commission',$new_commission);
			}
			return true;
	}
	/*
		上级佣金;
	*/
	function create_shangjifee($shangjifee){
			$agent=$this->get('member',array('mid'=>$this->commission_order['mid'],'uniacid'=>$_SESSION['uniacid']));
			$commission_level=$agent['level_id'];
			if(!$agent['level_id']){
				return false;
			}else{
				$level=$this->get('level',array('id'=>$agent['level_id']));
				$level_id=$level['id'];
				$commissions=json_decode($agent['commissions'],true);
				$commission_per=$commissions['shangji'];
			}
			$money=0;
			$order_price=$shangjifee;		
			//先检查是否存在商品独立分红规则，如果存在则对订单相应商品部分金额单独计算分红
			$goods_commission=$this->commission_order['goodscommissions'];
			foreach($goods_commission as $gv){
				if($agent['level_id']==$gv['level_id']){
					$goods_commission_per=$gv['shangji'];
					$isgoodsper=1;
					if(substr($goods_commission_per,-1)=='%'){
							$money=intval($order_price*substr($goods_commission_per,0,-1))/100;
					}else{
							$money=intval($goods_commission_per);
					}
					break;
				}
			}			
			if(!$isgoodsper){
				if(substr($commission_per,-1)=='%'){
					$money=intval($order_price*substr($commission_per,0,-1))/100;
				}else{
					$money=intval($commission_per);
				}
			}
			if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$level_id,
					'orderid'=>$this->commission_order['orderid'],
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'type'=>2,
					'fen_futype'=>$this->commission_order['fen_futype'],
				);
				$this->insert('commission',$new_commission);
			}
			return true;
	}
	/*
		退款打款
	*/
	public function refund_pay($renwu_id,$noexi=false,$isstop=false,$paytype=''){
		//执行退款操作
		$refund=$this->pdo_get('renwu',array('id'=>$renwu_id));
		if(!$refund){
			return false;
		}
		$isnum=pdo_count('jy_renwushop_renwuorder',array('uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu_id),'id');
		if($isnum && !$isstop){
			exi('有人参与了，不能退款！','error');
		}
		$isfee=pdo_getsum('jy_renwushop_renwuorder',array('uniacid'=>$_SESSION['uniacid'],'renwuid'=>$renwu_id),'fee');
		if($isfee>0){
			$refund['fee'] -=$isfee;
		}
		$this->model('member')->recharge($refund['mid'],$refund['fee'],5,'退款');
		pdo_update('jy_renwushop_renwu',array('isdel'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$renwu_id));
		$params=array(
				'mid'=>$refund['mid'],
				'msg'=>'任务退款',
				'fee'=>$refund['fee'],
				'status'=>1,
				'renwuid'=>$renwu_id,
				'isadd'=>1,
		);
		$this->model('model')->set_feelog($params,4);
	}
}